<?php

/**
 * 分销中心（首页） - AM
 * 版本：2.0
 */

global $_W, $_GPC;
$openid = m('user')->getOpenid();

if ($_W['isajax']) {
    $member = m('member')->getMember($openid);

    $recordPrice = $this->model->getCommissionRecordPrice($openid, array( 'total', 'pay', 'ok' ));

    $member['commission_total'] = !empty( $recordPrice['total'] ) ? number_format($recordPrice['total'], 2) : 0;
    $member['commission_ok'] = !empty( $recordPrice['ok'] ) ? number_format($recordPrice['ok'], 2) : 0;
    $member['commission_pay'] = !empty( $recordPrice['pay'] ) ? number_format($recordPrice['pay'], 2) : 0;

    $cansettle = $member['commission_ok'] > 0 && $member['commission_ok'] >= floatval($this->set['withdraw']);
    $commission_ok = $member['commission_ok'];

    $agentTeams = $this->model->getCommissionTeamCount($openid, $this->set['level']);
    $member['agentcount'] = number_format($agentTeams['all'], 0);

    // 缓存本次结果集。
    $cacheKey = $_W['uniacid'].'_record_statistics_o_all_'.$member['id'];
    $orderStatistics = WsCache::getCache($cacheKey);

    if(empty($orderStatistics)){
        $orderStatistics = $this->model->getCommissionRecordStatistics($openid, array( 'o_status_0', 'o_status_1', 'o_status_2', 'o_status_3' ));
        WsCache::setCache($cacheKey, $orderStatistics);
    }

    $member['ordercount0'] = 0;

    foreach ($orderStatistics as $statistic) {
        $member['ordercount0'] += intval($statistic['order_count']);
    }

    if (mb_strlen($member['nickname'], 'utf-8') > 6) {
        $member['nickname'] = mb_substr($member['nickname'], 0, 6, 'utf-8');
    }

    $openselect = false;

    if ($this->set['select_goods'] == '1') {
        if (empty( $member['agentselectgoods'] ) || $member['agentselectgoods'] == 2) {
            $openselect = true;
        }
    } else {
        if ($member['agentselectgoods'] == 2) {
            $openselect = true;
        }
    }

    $member['agenttime'] = date('Y-m-d H:i:s', $member['agenttime']);

    $this->set['openselect'] = $openselect;

    $level = $this->model->getLevel($openid);

    show_json(1, array( 'commission_ok' => $commission_ok, 'member' => $member, 'level' => $level, 'cansettle' => $cansettle, 'settlemoney' => number_format(floatval($this->set['withdraw']), 2), 'set' => $this->set ));
}

include $this->template('index');